<?php

class InsuranceAttendDao
{

    /**
     * Retrieve insurance attend list
     * @returns doctrine collection
     * @throws DaoException
     */
    public function getInsuranceAttendList($limit = 50, $offset = 0, $orderBy = 'id', $order = 'DESC')
    {
        try {
            $q = Doctrine_Query :: create()
                ->from('InsuranceAttend')
                ->orderBy($orderBy . " " . $order)
                ->offset($offset)
                ->limit($limit);
            return $q->execute();
        } catch (Exception $e) {
            throw new DaoException($e->getMessage());
        }
    }

    public function saveInsuranceAttend(InsuranceAttend $insuranceAttend)
    {
        try {
            if ($insuranceAttend->getId() == '') {
                $idGenService = new IDGeneratorService();
                $idGenService->setEntity($insuranceAttend);
                $insuranceAttend->setId($idGenService->getNextID());
            }

            $insuranceAttend->save();
            return true;
        } catch (Exception $e) {
            throw new DaoException($e->getMessage());
        }
    }

    public function getInsuranceAttendById($insuranceAttendById)
    {
        try {
            return Doctrine :: getTable('InsuranceAttend')->find($insuranceAttendById);
        } catch (Exception $e) {
            throw new DaoException($e->getMessage());
        }
    }

    public function searchInsuranceAttend($searchParams)
    {

        $insuranceAttendID = $searchParams['insuranceAttendID'];
        $insuranceAttendName = $searchParams['insuranceAttendName'];
        $insuranceAttendCreationDate = $searchParams['insuranceAttendCreationDate'];
        $orderField = (!empty($searchParams['orderField'])) ? $searchParams['orderField'] : 'ia.name';
        $orderBy = (!empty($searchParams['orderBy'])) ? $searchParams['orderBy'] : 'ASC';
        $noOfRecords = $searchParams['noOfRecords'];
        $offset = $searchParams['offset'];

        $sortQuery = $orderField . " " . $orderBy;

        $q = Doctrine_Query::create()->from('InsuranceAttend ia');

        if (!empty($insuranceAttendID)) {
            $q->addwhere('ia.id = ?', $insuranceAttendID);
        }
        if (!empty($insuranceAttendName)) {
            $q->addwhere('ia.name = ?', $insuranceAttendName);
        }
        if (!empty($insuranceAttendCreationDate)) {
            $q->addwhere('ia.creationDate = ?', $insuranceAttendCreationDate);
        }

        $q->orderBy($sortQuery);
        $q->offset($offset);
        $q->limit($noOfRecords);

        $insuranceAttendList = $q->execute();
        return $insuranceAttendList;
    }

    public function searchInsuranceAttendCount($searchParams)
    {
        try {

            $insuranceAttendID = $searchParams['insuranceAttendID'];
            $insuranceAttendName = $searchParams['insuranceAttendName'];
            $insuranceAttendCreationDate = $searchParams['insuranceAttendCreationDate'];

            $q = Doctrine_Query::create()->from('InsuranceAttend ia');

            if (!empty($insuranceAttendID)) {
                $q->addwhere('ia.id = ?', $insuranceAttendID);
            }
            if (!empty($insuranceAttendName)) {
                $q->addwhere('ia.name = ?', $insuranceAttendName);
            }
            if (!empty($insuranceAttendCreationDate)) {
                $q->addwhere('ia.creationDate = ?', $insuranceAttendCreationDate);
            }

            $results = $q->fetchArray();
            $count = $results[0]['COUNT'];
            return $count;

        } catch (Exception $e) {
            throw new DaoException($e->getMessage(), $e->getCode(), $e);
        }
    }
}